package com.lagou.web.servlet;

import com.lagou.utils.UUIDUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //1.创建磁盘文件工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2.创建文件上传核心类
            ServletFileUpload  upload = new ServletFileUpload(factory);
            //2.1设置上传文件的编码
            upload.setHeaderEncoding("UTF-8");
            //2.2判断表单是否是文件上传表单
            boolean multipartContent = upload.isMultipartContent(req);

            //2.3  如果是文件上传表单
            if(multipartContent){
                //3.解析request  --获取表单项的集合
                List<FileItem> list = upload.parseRequest(req);
                if(null != list){
                    //4.遍历集合   获取表单项
                    for (FileItem fileItem : list) {
                        //5.判断是普通的表单项还是文件上传项
                        boolean formField = fileItem.isFormField();
                        if(formField){
                            //普通表单项
                            String fieldName = fileItem.getFieldName();
                            String fildValue = fileItem.getString("UTF-8");//设置编码  防止乱码

                            System.out.println(fieldName+"="+fildValue);
                        }else{
                            //文件上传项
                            //获取文件名
                            String fileName = fileItem.getName();

                            //拼接新的文件名   使用UUID保证不重复
                            String newFileName = UUIDUtils.getUUID()+"_"+fileName;

                            //获取输入流
                            InputStream inputStream = fileItem.getInputStream();

                            //获取输出流
                            //1.获取项目的运行目录 G:\apache-tomcat-8.5.55-windows-x64\apache-tomcat-8.5.55\webapps\lagou_edu_home_war
                            String realPath = this.getServletContext().getRealPath("/");
                            //2.截取到webapp目录路径
                            String webappPath = realPath.substring(0, realPath.indexOf("lagou_edu_home_war"));
                            //3.拼接输出路径 将图片保存到upload
                            FileOutputStream fileOutputStream = new FileOutputStream(webappPath+"/upload/"+newFileName);

                            //使用IOUtils完成 文件的copy
                            IOUtils.copy(inputStream,fileOutputStream);

                            //关闭流
                            fileOutputStream.close();
                            inputStream.close();
                        }
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
